package com.freight.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.core.common.utils.PageUtil;
import com.freight.core.common.utils.ResultUtil;
import com.freight.core.common.vo.PageVo;
import com.freight.core.common.vo.Result;
import com.freight.file.vo.PicFileVo;
import com.freight.user.dto.VehicleLicenseDto;
import com.freight.user.entity.VehicleLicense;
import com.freight.user.service.VehicleLicenseService;
import com.freight.user.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.freight.user.dto.VehicleDetailInfoDto;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author huyingtai
 */
@Slf4j
@RestController
@Api(description = "行驶证 --- 车辆信息接口")
@RequestMapping("/freight/vehicleLicense")
@Transactional
public class UserVehicleLicenseController {

    @Autowired
    private VehicleLicenseService vehicleLicenseService;

    /**
     * 获取最新位置
     */
    @RequestMapping(value = "/vehicleposition", method = RequestMethod.POST)
    @ApiOperation(value = "车辆定位 --- 获取最新位置")
    public  Result<JSONObject> vehicleposition(String vehicleId) {
        JSONObject jsonObject=vehicleLicenseService.vehicleposition(vehicleId);
        return new ResultUtil<JSONObject>().setData(jsonObject);
    }

    @RequestMapping(value = "/historyVehicle", method = RequestMethod.POST)
    @ApiOperation(value = "历史轨迹 --- 获取车牌号")
    public  Result<List<HistoryVehicleVo>> historyVehicle(String vehicleNum) {
        List<HistoryVehicleVo> historyVehicle=vehicleLicenseService.historyVehicle(vehicleNum);
        return new ResultUtil<List<HistoryVehicleVo>>().setData(historyVehicle);
    }

    @RequestMapping(value = "/getVehicleGpalInfoVo", method = RequestMethod.POST)
    @ApiOperation(value = "根据设备号获取车辆相关信息")
    public Result<VehicleGpalInfoVo> getVehicleGpalInfoVo(String gpsEquipment) {
        VehicleGpalInfoVo res = vehicleLicenseService.getVehicleGpalInfoVo(gpsEquipment);
        return ResultUtil.data(res);
    }

    @RequestMapping(value = "/getPcDriverLicenseId", method = RequestMethod.POST)
    @ApiOperation(value = "PC端 --- 根据id获取车辆相关信息")
    public Result<PlaterVehicleLicenseVo> getPcDriverLicenseId(Long vehicleId) {
        PlaterVehicleLicenseVo res = vehicleLicenseService.getPcDriverLicenseId(vehicleId);
        return ResultUtil.data(res);
    }

    @RequestMapping(value = "/getVehicleLicenseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取行驶证相关信息")
    public Result<VehicleLicense> getVehicleLicenseInfo(Long userId) {
        VehicleLicense res = vehicleLicenseService.getVehicleLicenseInfo(userId);
        return ResultUtil.data(res);
    }

    @RequestMapping(value = "/getDriverLicenseId", method = RequestMethod.POST)
    @ApiOperation(value = "根据车辆id获取车辆信息")
    public Result<VehiceInfoVo> getDriverLicenseId(Long vehicleId) {
        VehiceInfoVo res = vehicleLicenseService.getDriverLicenseId(vehicleId);
        return ResultUtil.data(res);
    }

    @RequestMapping(value = "/uploadVehiclePic", method = RequestMethod.POST)
    @ApiOperation(value = "解析行驶证")
    public Result<PicFileVo> uploadVehiclePic(@RequestParam(required = false) MultipartFile file, String fileType) {
        PicFileVo res = vehicleLicenseService.uploadVehiclePic(file, fileType);
        return ResultUtil.data(res);
    }

    @RequestMapping(value = "/saveVehicleLicenseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "保存行驶证 ----- 车辆信息")
    public Result<String> saveWorkLicenseInfo(VehicleLicenseDto vehicleLicense) {
        vehicleLicenseService.saveVehicleLicenseInfo(vehicleLicense);
        return ResultUtil.data("保存成功");
    }

    @RequestMapping(value = "/updateGpsEquipment", method = RequestMethod.POST)
    @ApiOperation(value = "设置车辆GPS设备号 -----")
    public Result<String> updateGpsEquipment(Long vehicldId,String gpsEquipment) {
        vehicleLicenseService.updateGpsEquipment(vehicldId,gpsEquipment);
        return new ResultUtil().setData("操作成功");
    }

    @RequestMapping(value = "/vehicleRegistStatus", method = RequestMethod.GET)
    @ApiOperation(value = "车辆审核认证接口 -----")
    public Result<String> vehicleRegistStatus(Long vehicldId,Integer registState,String reason) {
        String res = vehicleLicenseService.vehicleRegistStatus(vehicldId,registState,reason);
        return new ResultUtil().setData(res);
    }

    @RequestMapping(value = "/vehicleManagementList", method = RequestMethod.GET)
    @ApiOperation(value = "平台端 --- 车辆管理列表接口")
    public Result<IPage<VehicleDetailInfoVo>> vehicleManagementList(PageVo page,VehicleDetailInfoDto vehicleDetailInfoDto) {
        IPage<VehicleDetailInfoVo> vehicleDetailInfoVoList = vehicleLicenseService.vehicleManagementList(PageUtil.initMpPage(page),vehicleDetailInfoDto);
        return new ResultUtil<IPage<VehicleDetailInfoVo>>().setData(vehicleDetailInfoVoList);
    }

    @RequestMapping(value = "/companyVehicleManagementList", method = RequestMethod.GET)
    @ApiOperation(value = "货主端 --- 车辆管理列表接口")
    public Result<IPage<VehicleDetailInfoVo>> companyVehicleManagementList(PageVo page,VehicleDetailInfoDto vehicleDetailInfoDto) {
        IPage<VehicleDetailInfoVo> vehicleDetailInfoVoList = vehicleLicenseService.companyVehicleManagementList(PageUtil.initMpPage(page),vehicleDetailInfoDto);
        return new ResultUtil<IPage<VehicleDetailInfoVo>>().setData(vehicleDetailInfoVoList);
    }

    @RequestMapping(value = "/cancellationVehicle", method = RequestMethod.GET)
    @ApiOperation(value = "车辆注销接口")
    public Result<String> cancellationVehicle(Long vehicleId,Integer registStatus) {
        vehicleLicenseService.cancellationVehicle(vehicleId,registStatus);
        return new ResultUtil().setData("");
    }

}
